package com.pan.interfere.impl;

import com.pan.interfere.Interfere;
import com.pan.utils.RandomColor;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CharInterfere implements Interfere{

    private BufferedImage bufferedImage;
    private Random random;
    private int width;
    private int height;
    private Graphics2D graphics;
    public CharInterfere(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
        width = bufferedImage.getWidth();
        height = bufferedImage.getHeight();
        random = new Random();
        graphics = bufferedImage.createGraphics();
    }

    public void resetRandomColor(){
        graphics.setColor(RandomColor.getR6Color());
    }
    /**
     *
     */
    public void randomLine() {
        resetRandomColor();
        graphics.drawLine(0,random.nextInt(height),width,random.nextInt(height));
        resetRandomColor();
        graphics.drawLine(random.nextInt(width),0,random.nextInt(width),height);
    }

    public void randomDot() {
        int w = random.nextInt(width);
        int h = random.nextInt(height);
        resetRandomColor();
        graphics.drawLine(w,h,w,h);
    }

    public void randomOval() {
        resetRandomColor();
        int ry = random.nextInt(height)-(height/2);
        int rx = random.nextInt(width)-(width/2);
        graphics.drawOval(rx,ry,width*2/3,height*2/3);
    }

    public void randomRect() {
        resetRandomColor();
        int ry = random.nextInt(height)-(height/2);
        int rx = random.nextInt(width)-(width/2);
        graphics.drawRect(rx,ry,width,height);
    }

    @Override
    public void drawInterfere() {
        randomLine();
        randomLine();
        randomDot();randomDot();
        randomDot();randomDot();
        randomDot();randomDot();
        randomDot();randomDot();
        randomDot();randomDot();
        randomOval();
        randomOval();
        randomRect();
        randomRect();
    }

    @Override
    public void close(){
        graphics.dispose();
    }
}
